;$(function () {

    layui.use(['form', 'laydate', 'table', 'element'], function () {
        var table = layui.table,
            form = layui.form,
            laydate = layui.laydate,
            element = layui.element;

        let nowDate = getNowAndFormatDate();
        let now = {};
        now[nowDate] = "今天";
        let weekDate = getWeekAndFormatDate();
        let showDate = weekDate + " 00:00:00 - " + nowDate + " 23:59:59";
        laydate.render({
            elem: '#priceChangeDate'
            , range: true
            , trigger: 'click'
            , type: 'datetime'
            , mark: now
            , value: showDate
            , done: function (value) {
                $("#priceChangeDate").val(value);
                $("#priceChangeSearchBtn").click();
            }
        });

        $(".layui-btn.layui-btn-primary.dateChoose").on('click', function () {
            let event = $(this).attr("lay-event");
            let nowDate = getNowAndFormatDate();
            switch (event) {
                case 'today':
                    $("#priceChangeDate").val(nowDate + " 00:00:00 - " + nowDate + " 23:59:59");
                    break;
                case 'yesterday':
                    let yesterday = getYesterdayAndFormatDate();
                    $("#priceChangeDate").val(yesterday + " 00:00:00 - " + yesterday + " 23:59:59");
                    break;
                case 'lastWeek':
                    let lastWeek = getWeekAndFormatDate();
                    $("#priceChangeDate").val(lastWeek + " 00:00:00 - " + nowDate + " 23:59:59");
                    break;
                case 'lastMonth':
                    let lastMonth = getMouthAndFormatDate();
                    $("#priceChangeDate").val(lastMonth + " 00:00:00 - " + nowDate + " 23:59:59");
                    break;
                case 'lastYear':
                    let lastYear = getYearAndFormatDate();
                    $("#priceChangeDate").val(lastYear + " 00:00:00 - " + nowDate + " 23:59:59");
                    break;
            }
            form.render();
            $("#priceChangeSearchBtn").click();
        });

        $("#priceChangeSearchBtn").on('click', function () {
            let searchDate = $("#priceChangeDate").val();
            let searchWaresId = $("#priceChangeSearchByWaresId").val();
            table.reload('priceChangeRender', {
                where: {
                    searchDate: searchDate
                    , searchWaresId: searchWaresId
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        table.render({
            elem: '#priceChangeTable',
            url: 'priceLog',
            method: "post",
            response: {
                statusCode: true
            },
            where: {
                searchDate: $("#priceChangeDate").val(),
                searchWaresId: $("#priceChangeSearchByWaresId").val()
            },
            defaultToolbar: [],
            cellMinWidth: 80,
            cols: [[
                {field: 'sorts', title: '序号', templet: '#sort', width: 58, align: 'center'}
                , {field: 'changeDate', title: '变动时间', align: 'center', width: 200}
                , {field: 'waresId', title: '商品条码', align: 'center'}
                , {field: 'waresName', title: '商品名称', align: 'center'}
                , {
                    field: 'price', title: '售价(元)', align: 'center', width: 100,
                    templet: function (d) {
                        return d.price.toFixed(2);
                    }
                }, {
                    field: 'originalPrice', title: '原进货价(元)', align: 'center', width: 120,
                    templet: function (d) {
                        return d.originalPrice.toFixed(2);
                    }
                }
                , {
                    field: 'presentPrice', title: '现进货价(元)', align: 'center', width: 120,
                    templet: function (d) {
                        return d.presentPrice.toFixed(2);
                    }
                }
                , {
                    field: 'change', title: '价格波动(元)', align: 'center', templet: function (d) {
                        if (d.originalPrice >= d.presentPrice) {
                            return '<span style="color:#13CE67;">' + (d.presentPrice - d.originalPrice).toFixed(2) + '↙</span>'
                        } else {
                            return '<span style="color:#D92E2E;">' + (d.presentPrice - d.originalPrice).toFixed(2) + '↗</span>'
                        }
                    }
                }
                , {
                    field: 'tool', title: '操作', align: 'center', width: 130,
                    templet: function (d) {
                        return '<span style="text-decoration:underline;cursor:pointer;color:#4BB2FF" ' +
                            'lay-event="edit">编辑售价</span>'
                    }
                }
            ]],
            id: 'priceChangeRender',
            page: true
        });

        table.on('tool(priceChangeTable)', function (obj) {
            let data = obj.data;
            if (obj.event === "edit") {
                layer.prompt({
                    formType: 0
                    , title: "修改商品售价为：(元)"
                    , value: data.price.toFixed(2)
                }, function (value, index) {
                    if (value != null) {
                        if (value < 0) {
                            layer.msg("不能小于0");
                        } else if (value >= 0) {
                            $.post('updateWaresPrice', {
                                id: data.waresId,
                                price: value
                            }, function (result) {
                                if (result.code === true) {
                                    table.reload('priceChangeRender', {
                                        where: {
                                            searchDate: $("#priceChangeDate").val(),
                                            searchWaresId: $("#priceChangeSearchByWaresId").val()
                                        },
                                        page: {
                                            curr: 1 //重新从第 1 页开始
                                        }
                                    });
                                    layer.close(index);
                                } else {
                                    layer.msg(result.msg);
                                }
                            });
                        } else {
                            layer.msg("无效数据");
                        }
                    }
                });
            }
        });


    });
});